package uk.co.gavin

class Person {
	
  // Contact Details
  
  String firstName;
  
  String lastName;
  
  String email;
  
  String phoneNumber;

  static hasMany = [skills:Skill]

  // Additional information
  
  boolean core;
  
  boolean employedFullTime;
  
  String notes;

  static constraints = {
    id (visible:false)
    firstName(blank: false, nullable: false, maxSize: 12, matches: '[a-zA-Z]+');
    lastName(maxSize: 12, blank: false, nullable: false, matches: '[a-zA-Z]+');
    email(unique:true, blank: false, nullable: false, email: true, maxSize: 20);
    phoneNumber(number: true, blank:false, nullable: false, maxSize: 12, matches: '[0-9]+');
    notes(maxSize: 1000)
    skills (blank:false)
    created (nullable:true)
    updated (nullable:true)
  }
  
  // Meta
  Date created
  Date updated
  
  def beforeInsert = {
	 created = new Date()
	 updated = created
  }
  
  def beforeUpdate = {
      updated = new Date()
  }

  
  public boolean hasSkillsWithIds(String... skillIds) {
	  if (skillIds == null || skillIds.length == 0) {
		  return true;
	  }
	  def allSkillIds = skills['id'];
	  for (def c : skillIds) {
		  if (!allSkillIds.contains(new Long(c))) {
			  return false;		  
		  }
	  }
	  return true;
  }
}
